//
//  YLExchangeDetailController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/7.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLExchangeDetailController.h"
#import "YLOrderDetailViewController.h"
#import "YLMallGoodModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "YLMyAdressModel.h"
#import "WHProgressHUD.h"
#import "YLOrderCodeModel.h"
#import <YYModel.h>
@interface YLExchangeDetailController ()
// 产品名称
@property (weak, nonatomic) IBOutlet UILabel *productNameLabel;
// 积分
@property (weak, nonatomic) IBOutlet UILabel *neetPointLabel;
// 数量
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
// 总积分
@property (weak, nonatomic) IBOutlet UILabel *totalNeetPointLabel;

@end

@implementation YLExchangeDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _productNameLabel.text = _model.pointProductName;
    _neetPointLabel.text = _model.neetPoint;
    _numberLabel.text = _model.count;
    _totalNeetPointLabel.text = _model.totalNeetPoint;
    
    
}

- (IBAction)exchangeSure:(UIButton *)sender {
    
    if (!_model.addressModel) {
        [WHProgressHUD showError:@"请选择收货地址"];
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@Point/PCreatePintOrder", YLURL];
    
    sender.userInteractionEnabled = NO;
    [sender setTitle:@"兑换中" forState:UIControlStateNormal];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:3];
    params[@"ProductId"] = _model.pointProductId;
    params[@"PointProductAmount"] = _model.count;
    params[@"RecieveId"] = _model.addressModel.receiveAddressId;
    
    __weak typeof(self) weakSelf = self;
    [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
        sender.userInteractionEnabled = YES;
        [sender setTitle:@"确认兑换" forState:UIControlStateNormal];
        if ([responseObject[@"httpCode"] integerValue] == 200) {
            YLOrderCodeModel *model =  [YLOrderCodeModel yy_modelWithDictionary:responseObject[@"httpResult"]];
            // 跳转
            YLOrderDetailViewController *detail = [[YLOrderDetailViewController alloc] init];
            detail.addressModel = weakSelf.model.addressModel;
            detail.orderCode = model.pointOrderCode;
            [self.navigationController pushViewController:detail animated:YES];
            
        } else {
            [WHProgressHUD showError:responseObject[@"message"]];
        }
    } fail:^(NSError *error) {
        sender.userInteractionEnabled = YES;
        [sender setTitle:@"确认兑换" forState:UIControlStateNormal];
    }];
    
    
   
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
